{% extends "base.html" %}

{% block content %}
    <h1>结算列表</h1>

    <form method="get" class="mb-3">
        <div class="row">
            <div class="col-md-3">
                <select name="source" class="form-select">
                    <option value="">所有来源</option>
                    {% for value, name in source_choices %}
                        <option value="{{ value }}" {% if request.GET.source == value|stringformat:"i" %}selected{% endif %}>{{ name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <select name="category" class="form-select">
                    <option value="">所有类别</option>
                    {% for category in categories %}
                        <option value="{{ category }}" {% if request.GET.category == category %}selected{% endif %}>{{ category }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <select name="status" class="form-select">
                    <option value="">所有状态</option>
                    {% for value, name in status_choices %}
                        <option value="{{ value }}" {% if request.GET.status == value|stringformat:"i" %}selected{% endif %}>{{ name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <input type="text" name="mac" class="form-control" placeholder="搜索 MAC 码" value="{{ request.GET.mac }}">
            </div>
        </div>
        <div class="mt-3">
            <button type="submit" class="btn btn-primary">筛选/搜索</button>
            <a href="{% url 'settlement_list' %}" class="btn btn-secondary">清除筛选</a>
        </div>
    </form>


    <table class="table">
        <thead>
            <tr>
                <th>MAC码</th>
                <th>来源</th>
                <th>类别</th>
                <th>购置日期</th>
                <th>翻新日期</th>
                <th>翻新次</th>
                <th>检查次</th>
                <th>映射错误次</th>
                <th>未纳管次</th>
                <th>在线次</th>
                <th>离线次</th>
                <th>人工标注</th>
                <th>状态</th>
                <th>编辑</th>
            </tr>
        </thead>
        <tbody>
            {% for settlement in page_obj.object_list %}
                <tr>
                    <td>{{ settlement.mac }}</td>
                    <td>{{ settlement.get_source_display }}</td>
                    <td>{{ settlement.category }}</td>
                    <td>{{ settlement.date0 }}</td>
                    <td>{{ settlement.date1 }}</td>
                    <td>{{ settlement.fix }}</td>
                    <td>{{ settlement.times }}</td>
                    <td>{{ settlement.mismapping }}</td>
                    <td>{{ settlement.blank }}</td>
                    <td>{{ settlement.online }}</td>
                    <td>{{ settlement.offline }}</td>
                    <td>{{ settlement.manual }}</td>
                    <td>{{ settlement.get_status_display }}</td>
                    <td>
                        <a href="{% url 'edit_settlement' settlement.pk %}" class="btn btn-sm btn-primary">编辑</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    <!-- 分页导航 -->
    {% if page_obj.has_other_pages %}
        <nav aria-label="Page navigation">
            <ul class="pagination">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?{% if request.GET.source %}source={{ request.GET.source }}&{% endif %}{% if request.GET.category %}category={{ request.GET.category }}&{% endif %}{% if request.GET.status %}status={{ request.GET.status }}&{% endif %}{% if request.GET.mac %}mac={{ request.GET.mac }}&{% endif %}page={{ page_obj.previous_page_number }}">上一页</a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">上一页</span>
                    </li>
                {% endif %}

                {% for i in page_obj.paginator.page_range %}
                    {% if page_obj.number == i %}
                        <li class="page-item active" aria-current="page">
                            <span class="page-link">{{ i }}</span>
                        </li>
                    {% else %}
                        <li class="page-item">
                            <a class="page-link" href="?{% if request.GET.source %}source={{ request.GET.source }}&{% endif %}{% if request.GET.category %}category={{ request.GET.category }}&{% endif %}{% if request.GET.status %}status={{ request.GET.status }}&{% endif %}{% if request.GET.mac %}mac={{ request.GET.mac }}&{% endif %}page={{ i }}">{{ i }}</a>
                        </li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?{% if request.GET.source %}source={{ request.GET.source }}&{% endif %}{% if request.GET.category %}category={{ request.GET.category }}&{% endif %}{% if request.GET.status %}status={{ request.GET.status }}&{% endif %}{% if request.GET.mac %}mac={{ request.GET.mac }}&{% endif %}page={{ page_obj.next_page_number }}">下一页</a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">下一页</span>
                    </li>
                {% endif %}
            </ul>
        </nav>
    {% endif %}
{% endblock %}